//
//  BCOrderViewController.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/13.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCOrderViewController.h"
#import "BCOrderFirstCollectionViewCell.h"
#import "BCOrderSecondSiderCollectionViewCell.h"
#import "BCOrderSecondCollectionViewCell.h"
#import "BCOrderThirdCollectionViewCell.h"

@interface BCOrderViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
//订单种类选择视图
@property (nonatomic, strong) UIView *segmentView;
//订单种类选择按钮背景
@property (nonatomic, strong) UIView *segmentItem_0_BackgroundView;
@property (nonatomic, strong) UIView *segmentItem_1_BackgroundView;
@property (nonatomic, strong) UIView *segmentItem_2_BackgroundView;
//订单种类选择按钮
@property (nonatomic, strong) UIButton *segmentItem_0;
@property (nonatomic, strong) UIButton *segmentItem_1;
@property (nonatomic, strong) UIButton *segmentItem_2;
//三订单滚动视图
@property (nonatomic, strong) UIScrollView *mainScrollView;
//三订单背景视图
@property (nonatomic, strong) UIView *aboveScroll_0_View;
@property (nonatomic, strong) UIView *aboveScroll_1_View;
@property (nonatomic, strong) UIView *aboveScroll_2_View;
//支付横条视图
@property (nonatomic, strong) UIView *payView;
@property (nonatomic, strong) UILabel *countLable;
@property (nonatomic, strong) UILabel *priceLable;
@property (nonatomic, strong) UIButton *payButton;
//三订单列表视图-1
@property (nonatomic, strong) UICollectionView *orderList_0_CollectionView;
//三订单列表视图-2
@property (nonatomic, strong) UICollectionView *orderList_1_CollectionView;
@property (nonatomic, strong) UICollectionView *orderList_1_Sider_CollectionView;
//三订单列表视图-3
@property (nonatomic, strong) UICollectionView *orderList_2_CollectionView;
//三订单滚动页
@property (nonatomic, assign) NSInteger page;

@end

@implementation BCOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [[self rdv_tabBarController] setTabBarHidden:!self.rdv_tabBarController.tabBarHidden animated:YES];
    [self p_initNavgationBar];
    [self p_initSegmentView];
    [self p_initMainScroll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat x = scrollView.contentOffset.x;
    _page = x/scrollView.frame.size.width;
    if (_page==0) {
        self.segmentItem_0_BackgroundView.hidden = NO;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = YES;
    }
    else if (_page==1) {
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = NO;
        self.segmentItem_2_BackgroundView.hidden = YES;
    }
    else if (_page==2){
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = NO;
    }

}

//拖动结束
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    CGFloat x = scrollView.contentOffset.x;
    

}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
}

#pragma mark - Private Action
- (void)rightItemAction:(UIBarButtonItem *)item {
}

- (void)segmentAction:(UIButton *)item {
    if (0 == item.tag) {
        self.segmentItem_0_BackgroundView.hidden = NO;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = YES;
        self.mainScrollView.contentOffset = CGPointMake(0, 0);
    }
    else if (1 == item.tag) {
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = NO;
        self.segmentItem_2_BackgroundView.hidden = YES;
        self.mainScrollView.contentOffset = CGPointMake(self.mainScrollView.frame.size.width, 0);
    }
    else {
        self.segmentItem_0_BackgroundView.hidden = YES;
        self.segmentItem_1_BackgroundView.hidden = YES;
        self.segmentItem_2_BackgroundView.hidden = NO;
        self.mainScrollView.contentOffset = CGPointMake(self.mainScrollView.frame.size.width*2, 0);
    }
}

- (void)payAction:(UIButton *)item {
    NSLog(@"pay action");
}

#pragma mark - View init
- (void)p_initNavgationBar {
    self.title = @"订单";
    self.navigationController.navigationBar.barTintColor = BCColor(0.19, 0.63, 0.34, 1);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(rightItemAction:)];
}

- (void)p_initSegmentView {
    self.segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44.f)];
    self.segmentView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.view addSubview:self.segmentView];
    self.segmentItem_0_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.segmentView.frame.size.width/3, self.segmentView.frame.size.height)];
    self.segmentItem_0_BackgroundView.backgroundColor = BCColor(0.19, 0.63, 0.34, 1);
    self.segmentItem_0_BackgroundView.hidden = NO;
    self.segmentItem_0 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.segmentItem_0.frame = CGRectMake(0, 0, self.segmentItem_0_BackgroundView.frame.size.width, self.segmentItem_0_BackgroundView.frame.size.height-3.f);
    [self.segmentItem_0 setTitle:@"今日订单" forState:UIControlStateNormal];
    [self.segmentItem_0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.segmentItem_0 setBackgroundColor:[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1]];
    self.segmentItem_0.tag = 0;
    [self.segmentItem_0 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.segmentView addSubview:self.segmentItem_0_BackgroundView];
    [self.segmentView addSubview:self.segmentItem_0];
    self.segmentItem_1_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width, 0, self.segmentView.frame.size.width/3, self.segmentView.frame.size.height)];
    self.segmentItem_1_BackgroundView.backgroundColor = BCColor(0.19, 0.63, 0.34, 1);
    self.segmentItem_1_BackgroundView.hidden = YES;
    self.segmentItem_1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.segmentItem_1.frame = CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width, 0, self.segmentItem_1_BackgroundView.frame.size.width, self.segmentItem_1_BackgroundView.frame.size.height-3.f);
    [self.segmentItem_1 setTitle:@"常购订单" forState:UIControlStateNormal];
    [self.segmentItem_1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.segmentItem_1 setBackgroundColor:[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1]];
    self.segmentItem_1.tag = 1;
    [self.segmentItem_1 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.segmentView addSubview:self.segmentItem_1_BackgroundView];
    [self.segmentView addSubview:self.segmentItem_1];
    self.segmentItem_2_BackgroundView = [[UIView alloc] initWithFrame:CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width*2, 0, self.segmentView.frame.size.width/3, self.segmentView.frame.size.height)];
    self.segmentItem_2_BackgroundView.backgroundColor = BCColor(0.19, 0.63, 0.34, 1);
    self.segmentItem_2_BackgroundView.hidden = YES;
    self.segmentItem_2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.segmentItem_2.frame = CGRectMake(self.segmentItem_0_BackgroundView.frame.size.width*2, 0, self.segmentItem_2_BackgroundView.frame.size.width, self.segmentItem_2_BackgroundView.frame.size.height-3.f);
    [self.segmentItem_2 setTitle:@"历史订单" forState:UIControlStateNormal];
    [self.segmentItem_2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.segmentItem_2 setBackgroundColor:[UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1]];
    self.segmentItem_2.tag = 2;
    [self.segmentItem_2 addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.segmentView addSubview:self.segmentItem_2_BackgroundView];
    [self.segmentView addSubview:self.segmentItem_2];
}

- (void)p_initMainScroll {
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.segmentView.frame.size.height+64.f, self.view.frame.size.width, self.view.frame.size.height-self.segmentView.frame.size.height-50.f-60.f)];
    self.mainScrollView.delegate = self;
    self.mainScrollView.backgroundColor = [UIColor grayColor];
    
    self.aboveScroll_0_View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height)];
    self.aboveScroll_0_View.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.aboveScroll_0_View];
    [self p_initMainScrollFirstView];
    
    self.aboveScroll_1_View = [[UIView alloc] initWithFrame:CGRectMake(self.mainScrollView.frame.size.width, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height)];
    self.aboveScroll_1_View.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.aboveScroll_1_View];
    [self p_initMainScrollSecondView];
    
    self.aboveScroll_2_View = [[UIView alloc] initWithFrame:CGRectMake(self.mainScrollView.frame.size.width*2, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height)];
    self.aboveScroll_2_View.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.aboveScroll_2_View];
    [self p_initMainScrollThirdView];
    
    self.mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, self.mainScrollView.frame.size.height);
    self.mainScrollView.pagingEnabled =YES;
//    self.mainScrollView.scrollEnabled = NO;
    
    [self.view addSubview:self.mainScrollView];
}

- (void)p_initMainScrollFirstView {
    self.payView = [[UIView alloc] initWithFrame:CGRectMake(0, self.aboveScroll_0_View.frame.size.height-55.f, self.aboveScroll_0_View.frame.size.width, 60.f)];
    self.payView.backgroundColor = [UIColor colorWithRed:248.f/255.f green:248.f/255.f blue:248.f/255.f alpha:1];
    
    UILabel *countTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(8.f, 5.f, 45.f, 50.f)];
    countTitleLable.font = [UIFont systemFontOfSize:15];
    countTitleLable.text = @"已选：";
    countTitleLable.textColor = [UIColor blackColor];
    [self.payView addSubview:countTitleLable];
    self.countLable = [[UILabel alloc] initWithFrame:CGRectMake(50.f, 5.f, 80.f, 50.f)];
    self.countLable.font = [UIFont systemFontOfSize:15];
    self.countLable.text = @"2种商品";
    self.countLable.textColor = [UIColor orangeColor];
    [self.payView addSubview:self.countLable];
    UILabel *priceTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(130.f, 5.f, 60.f, 50.f)];
    priceTitleLable.font = [UIFont systemFontOfSize:15];
    priceTitleLable.text = @"预估价：";
    priceTitleLable.textColor = [UIColor blackColor];
    [self.payView addSubview:priceTitleLable];
    self.priceLable = [[UILabel alloc] initWithFrame:CGRectMake(190.f, 5.f, 80.f, 50.f)];
    self.priceLable.font = [UIFont systemFontOfSize:15];
    self.priceLable.text = @"65元";
    self.priceLable.textColor = [UIColor orangeColor];
    [self.payView addSubview:self.priceLable];
    self.payButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.payButton.frame = CGRectMake(self.payView.frame.size.width-98.f, 0, 98.f, self.payView.frame.size.height);
    self.payButton.backgroundColor = [UIColor orangeColor];
    [self.payButton setTitle:@"去支付" forState:UIControlStateNormal];
    [self.payButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.payButton addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.payView addSubview:self.payButton];
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.orderList_0_CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.aboveScroll_0_View.frame.size.width, self.aboveScroll_0_View.frame.size.height) collectionViewLayout:flowLayout];
    self.orderList_0_CollectionView.backgroundColor = [UIColor whiteColor];
    self.orderList_0_CollectionView.delegate = self;
    self.orderList_0_CollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.orderList_0_CollectionView.contentInset = UIEdgeInsetsMake(0, 0, 55, 0);
    [self.orderList_0_CollectionView registerClass:[BCOrderFirstCollectionViewCell class] forCellWithReuseIdentifier:@"FirstCell"];
    self.orderList_0_CollectionView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.aboveScroll_0_View addSubview:self.orderList_0_CollectionView];
    [self.aboveScroll_0_View addSubview:self.payView];
}

- (void)p_initMainScrollSecondView {
    UICollectionViewFlowLayout *sider_flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.orderList_1_Sider_CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100.f, self.aboveScroll_0_View.frame.size.height) collectionViewLayout:sider_flowLayout];
    self.orderList_1_Sider_CollectionView.delegate = self;
    self.orderList_1_Sider_CollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.orderList_1_Sider_CollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.orderList_1_Sider_CollectionView registerClass:[BCOrderSecondSiderCollectionViewCell class] forCellWithReuseIdentifier:@"SecondSiderCell"];
    self.orderList_1_Sider_CollectionView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.aboveScroll_1_View addSubview:self.orderList_1_Sider_CollectionView];
    
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.orderList_1_CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.orderList_1_Sider_CollectionView.frame.size.width, 0, self.aboveScroll_1_View.frame.size.width-self.orderList_1_Sider_CollectionView.frame.size.width, self.aboveScroll_1_View.frame.size.height) collectionViewLayout:flowLayout];
    self.orderList_1_CollectionView.delegate = self;
    self.orderList_1_CollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.orderList_1_CollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.orderList_1_CollectionView registerClass:[BCOrderSecondCollectionViewCell class] forCellWithReuseIdentifier:@"SecondCell"];
    self.orderList_1_CollectionView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.aboveScroll_1_View addSubview:self.orderList_1_CollectionView];
}

- (void)p_initMainScrollThirdView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.orderList_2_CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.aboveScroll_2_View.frame.size.width, self.aboveScroll_2_View.frame.size.height) collectionViewLayout:flowLayout];
    self.orderList_2_CollectionView.backgroundColor = [UIColor whiteColor];
    self.orderList_2_CollectionView.delegate = self;
    self.orderList_2_CollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.orderList_2_CollectionView.contentInset = UIEdgeInsetsMake(0, 0, 55, 0);
    [self.orderList_2_CollectionView registerClass:[BCOrderThirdCollectionViewCell class] forCellWithReuseIdentifier:@"ThirdCell"];
    self.orderList_2_CollectionView.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
    [self.aboveScroll_2_View addSubview:self.orderList_2_CollectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([collectionView isEqual:self.orderList_0_CollectionView]) {
        return 10;
    }
    else if ([collectionView isEqual:self.orderList_1_CollectionView]) {
        return 10;
    }
    else if ([collectionView isEqual:self.orderList_2_CollectionView]) {
        return 3;
    }

    else if ([collectionView isEqual:self.orderList_1_Sider_CollectionView]) {
        return 3;
    }
    else {
        return 10;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.orderList_0_CollectionView]) {
        static NSString *ID = @"FirstCell";
        BCOrderFirstCollectionViewCell *cell = [BCOrderFirstCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }
    else if ([collectionView isEqual:self.orderList_1_CollectionView]) {
        static NSString *ID = @"SecondCell";
        BCOrderSecondCollectionViewCell *cell = [BCOrderSecondCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }
    else if ([collectionView isEqual:self.orderList_2_CollectionView]) {
        static NSString *ID = @"ThirdCell";
        BCOrderThirdCollectionViewCell *cell = [BCOrderThirdCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        return cell;
    }
    else {
        static NSString *ID = @"SecondSiderCell";
        BCOrderSecondSiderCollectionViewCell *cell = [BCOrderSecondSiderCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.title.text = @"全部";
            cell.backgroundColor = [UIColor whiteColor];
        }
        else if (indexPath.row == 1) {
            cell.title.text = @"蔬菜";
        }
        else {
            cell.title.text = @"肉类";
        }

        return cell;

    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    
    UIEdgeInsets top = {0,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([collectionView isEqual:self.orderList_0_CollectionView]) {
        return CGSizeMake(self.view.frame.size.width, 102);
    }
    else if ([collectionView isEqual:self.orderList_1_CollectionView]) {
        return CGSizeMake(self.aboveScroll_1_View.frame.size.width-self.orderList_1_Sider_CollectionView.frame.size.width, 93);
    }
    else if ([collectionView isEqual:self.orderList_2_CollectionView]) {
        return CGSizeMake(self.view.frame.size.width, 80);
    }
    else if ([collectionView isEqual:self.orderList_1_Sider_CollectionView]) {
        return CGSizeMake(self.orderList_1_Sider_CollectionView.frame.size.width, 40.f);
    }
    else {
        return CGSizeMake(self.view.frame.size.width, 102);
    }

    
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 25;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if ([collectionView isEqual:self.orderList_0_CollectionView]) {
        return 5.f;
    }
    else if ([collectionView isEqual:self.orderList_1_CollectionView]) {
        return 5.f;
    }
    else if ([collectionView isEqual:self.orderList_2_CollectionView]) {
        return 5.f;
    }
    else {
        return 0.f;
    }
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cell %@,%@ selected",@(indexPath.section),@(indexPath.row));
    if ([collectionView isEqual:self.orderList_1_Sider_CollectionView]) {
        NSArray *cells = [collectionView visibleCells];
        [cells enumerateObjectsUsingBlock:^(BCOrderSecondSiderCollectionViewCell*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BCOrderSecondSiderCollectionViewCell *cell = (BCOrderSecondSiderCollectionViewCell *)obj;
            cell.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
        }];
        BCOrderSecondSiderCollectionViewCell *cell = (BCOrderSecondSiderCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
    }
}

@end
